package com.book.demo.Service.ServiceIMP;

import com.book.demo.Mapper.WendaMapper;
import com.book.demo.Pojo.Wenda;
import com.book.demo.Service.WendaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WendaServiceIMP implements WendaService {

    @Autowired
    private WendaMapper wendaMapper;

    /**
     * 获取所有问答，区分是否回复
     *
     * @return
     */
    @Override
    public Map<String, Object> getAllWendaIsReplyAndNotReply() {
        Map<String, Object> Result = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        try {
            data.put("isReply", wendaMapper.getAllReplyWenda());
            data.put("notReply", wendaMapper.getAllNoReplyWenda());
            Result.put("code", 200);
            Result.put("result", true);
            Result.put("msg", "获取问答成功");
            Result.put("data", data);

        } catch (Exception e) {
            e.printStackTrace();
            Result.put("code", 500);
            Result.put("msg", "获取问答失败,后台错误");
            Result.put("result", false);
        }
        return Result;
    }

    /**
     * @param pageNum
     * @param pageSize
     * @param gettype
     * @return 查询问答结果
     */
    @Override
    public Map<String, Object> GetWendasByPage(int pageNum, int pageSize, String gettype) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Wenda> wenda_list = wendaMapper.getWendasByPage(pageNum, pageSize, gettype);
            result.put("code", 200);
            result.put("result", true);
            result.put("msg", "获取问答成功");
            result.put("data", wenda_list);
            result.put("total", wenda_list.size());
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 500);
            result.put("msg", "获取问答失败,后台错误");
            result.put("result", false);
            result.put("data", null);
            result.put("total", 0);
        }
        return result;
    }

    /**
     * 获取问答总数
     *
     * @return Map<String, Object>
     */
    @Override
    public Map<String, Object> getWendaTotalCount() {
        Map<String, Object> result = new HashMap<>();
        try {
            result.put("code", 200);
            result.put("result", true);
            result.put("msg", "获取问答总数成功");
            result.put("data", wendaMapper.getAllWendaCount());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 500);
            result.put("msg", "获取问答总数失败,后台错误");
            result.put("result", false);
            result.put("data", 0);
        }
        return result;
    }

    /**
     * @param TheReplyWenda
     * @return
     */
    @Override
    public Map<String, Object> adminReplyWenda(Wenda TheReplyWenda) {
        System.out.println("问答数据：" + TheReplyWenda);
        Map<String, Object> result = new HashMap<>();
        try {
            if (wendaMapper.adminReplyWenda(TheReplyWenda) > 0) {
                result.put("code", 200);
                result.put("result", true);
                result.put("msg", "回复问答成功");
                result.put("data", TheReplyWenda.getAnswer());
            } else {
                result.put("code", 200);
                result.put("result", false);
                result.put("msg", "回复问答失败");
                result.put("data", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 500);
            result.put("msg", "回复问答失败,后台错误");
            result.put("result", false);
            result.put("data", null);
        }
        return result;
    }

}